#include "stack.h"

void StackInit(ST* st)
{
	assert(st);

	st->arr = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (st->arr == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	st->capacity = 4;
	st->top = 0;
}

void StackDestroy(ST* st)
{
	assert(st);

	free(st->arr);
	st->arr = NULL;
	st->capacity = st->top = 0;	
}

void StackPush(ST* st, STDataType x)
{
	assert(st);
	 
	if (st->top == st->capacity)
	{
		STDataType* temp = (STDataType*)realloc(st->arr, sizeof(STDataType) * st->capacity*2);
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			st->arr = temp;
			st->capacity *= 2;
		}
	}

	st->arr[st->top] = x;
	st->top++;
}

void StackPop(ST* st)
{
	assert(st);
	assert(st->top > 0);

	st->top--;
}

STDataType StackTop(ST* st)
{
	assert(st);

	return st->arr[st->top - 1];
}

int StackSize(ST* st)
{
	assert(st);

	return st->top;
}

bool StackEmpty(ST* st)
{
	assert(st);

	return st->top == 0;
}



